package com.itheima.youdianda.service.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.youdianda.common.R;
import com.itheima.youdianda.dto.YddAdvposDto;
import com.itheima.youdianda.exception.CommonException;
import com.itheima.youdianda.mapper.YddadvposMapper;
import com.itheima.youdianda.pojo.YddAdvimg;
import com.itheima.youdianda.pojo.YddAdvpos;
import com.itheima.youdianda.service.YddAdvimgService;
import com.itheima.youdianda.service.YddadvposService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class YddadvposServiceImpl extends ServiceImpl<YddadvposMapper, YddAdvpos> implements YddadvposService {
    @Autowired
    private YddAdvimgService yddAdvimgService;

    /**
     * 广告位列表分页
     *
     * @param page
     * @param limit
     * @param advposdesc
     * @param advposname
     * @return
     */
    @Override
    public R advposList(Integer page, Integer limit, String advposname, String advposdesc) {


        Page<YddAdvpos> yddAdvposPage = new Page<>(page, limit);

        //添加条件
        LambdaQueryWrapper<YddAdvpos> wrapper = new LambdaQueryWrapper<>();
        //添加条件 模糊查询条件 --广告位名称
        wrapper.like(StrUtil.isNotBlank(advposname), YddAdvpos::getAdvposname, advposname);
        //添加条件 模糊查询条件 --广告位描述
        wrapper.like(StrUtil.isNotBlank(advposname), YddAdvpos::getAdvposdesc, advposdesc);

        page(yddAdvposPage, wrapper);

        //查询广告图数量
        List<YddAdvpos> yddAdvposList = yddAdvposPage.getRecords();
        for (YddAdvpos yddAdvpos : yddAdvposList) {
            //获取广告位id,与广告图位置关联,在广告图中为String
            String strId = Convert.toStr(yddAdvpos.getId());
            //添加条件
            LambdaQueryWrapper<YddAdvimg> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(StrUtil.isNotBlank(strId), YddAdvimg::getAdvimgpos, strId);

            int count = yddAdvimgService.count(queryWrapper);

            yddAdvpos.setAdvposnum(count);
        }
       // yddAdvposPage.setRecords(yddAdvposList);
        YddAdvposDto yddAdvposDto = new YddAdvposDto();
        yddAdvposDto.setCount(Convert.toInt(yddAdvposPage.getTotal()));
        yddAdvposDto.setPageSize(Convert.toInt(yddAdvposPage.getSize()));
        yddAdvposDto.setTotalPages(Convert.toInt(yddAdvposPage.getPages()));
        yddAdvposDto.setCurrrntPage(Convert.toInt(yddAdvposPage.getCurrent()));
        yddAdvposDto.setData(yddAdvposPage.getRecords());

        return R.success(yddAdvposDto);
    }

    /**
     * 广告位添加
     *
     * @param yddAdvpos
     * @return
     */
    @Override
    public R advposAdd(YddAdvpos yddAdvpos) {
        //添加即为默认值
        // yddAdvpos.setDefaultData("1");

        this.save(yddAdvpos);
        return R.success(null);
    }

    /**
     * 广告位修改
     *
     * @param yddAdvpos
     * @return
     */
    @Override
    public R advposUpdate(YddAdvpos yddAdvpos) {
        this.updateById(yddAdvpos);
        return R.success(null);
    }

    /**
     * 广告位删除
     *
     * @param id
     * @return
     */
    @Override
    public R del(Integer id) {
        YddAdvpos yddAdvpos = this.getById(id);
        //获取默认值
        String defaultData = yddAdvpos.getDefaultData();
        //若默认值为1,不可以删除
        if ("1".equals(defaultData)) {
            return R.error("系统默认数据，不允许删除");
        }
        //删除
        this.removeById(id);
        return R.success("删除成功!");
    }

    /**
     * 广告位列表全部
     *
     * @return
     */
    @Override
    public R getAll() {
        List<YddAdvpos> advposList = list();
        return R.success(advposList);
    }

    /**
     * 广告位详情
     *
     * @param id
     * @return
     */
    @Override
    public R rowInfo(Integer id) {
        YddAdvpos yddAdvpos = this.getById(id);
        if (ObjectUtil.isEmpty(yddAdvpos)) {
            throw new CommonException("系统中无数据");
        }
        return R.success(yddAdvpos);
    }
}
